/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

/**
 * A field that contains a table for defining the description of the question's values
 */
qx.Class.define("bip.authorview.forms.QuestionTextValuesField",
{
  extend : qx.ui.core.Widget,
  implement : [
    qx.ui.form.IForm,
    qx.ui.form.IStringForm
  ],
  include : [
    qx.ui.form.MForm
  ],


  /*
  *****************************************************************************
     CONSTRUCTOR
  *****************************************************************************
  */

  /**
   * 
   */
  construct : function()
  {
    this.base(arguments);

		 // initialize the layout
    var layout = new qx.ui.layout.VBox(10);
    this._setLayout(layout);
    
    this._createChildControl("table");
    this._createChildControl("info");
    
  },


  /*
  *****************************************************************************
     EVENTS
  *****************************************************************************
  */


  events :
  {
    /** Fired when the value was modified */
    "changeValue" : "qx.event.type.Data"
  },


  /*
  *****************************************************************************
     PROPERTIES
  *****************************************************************************
  */


  properties :
  {
    // overridden
    appearance :
    {
      refine : true,
      init : "table"
    }
  
  },


  /*
  *****************************************************************************
     MEMBERS
  *****************************************************************************
  */


  members :
  {

		/*
		 * ****************************************************************************
		 * UI
		 * ****************************************************************************
		 */
		// overridden
		_createChildControlImpl : function(id) {
			var control;
			switch (id) {
			
	      case "info":
	        control = new qx.ui.basic.Atom(this.tr("<i>If you've changed the min/max values,click update to synchronize table content</i>"));
	        control.setAnonymous(true);
	        control.setRich(true);
	        control.setAllowGrowX(true);
	        this._add(control);
	        break;
			
				case "table":
					var tableModel = new qx.ui.table.model.Simple();
		    	tableModel.setColumns([ "Value", "Description" ]);
		    	tableModel.setColumnEditable(1, true);
		    	tableModel.setColumnEditable(2, true);
		    	control = new qx.ui.table.Table(tableModel);
		    	control.set({
		        width: 400,
		        height: 200,
		        decorator : null
		      });
		    	var tcm = control.getTableColumnModel();
		      tcm.setDataCellRenderer(0, new qx.ui.table.cellrenderer.Number());
		      tcm.setColumnWidth(0,50);
		      tcm.setDataCellRenderer(1, new qx.ui.table.cellrenderer.String());
		      tcm.setColumnWidth(1,300);
					
		      this._add(control);
					break;
          
			}

			return control || this.base(arguments, id);

		},
    
    /*
    ---------------------------------------------------------------------------
      VALUE PROPERTY
    ---------------------------------------------------------------------------
    */

    /**
     * Sets the element's value.
     *
     * @param value {String|null} The new value of the element.
     */
    setValue : function(value) {
    	var rows = [];
			for (var i=0; i<value.length;i++) {
				rows.push([value.getItem(i).getItem(0),value.getItem(i).getItem(1)]);
			}
			this.getChildControl("table").getTableModel().setData(rows);
    },


    /**
     * Resets the element's value to its initial value.
     */
    resetValue : function() {},


    /**
     * The element's user set value.
     *
     * @return {String|null} The value.
     */
    getValue : function() {
    	this.getChildControl("table").stopEditing();
    	var rows = this.getChildControl("table").getTableModel().getData();
    	var model = new qx.data.Array();
    	for (var i=0; i<rows.length;i++) {
				model.push(new qx.data.Array([rows[i][0],rows[i][1]]));
			}
    	return model;
    }

  },
  
  

  /*
  *****************************************************************************
     DESTRUCTOR
  *****************************************************************************
  */

  destruct : function() {
    this._disposeObjects("__content");
  }
});
